/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package id3.controllers;

import id3.models.RowData;
import id3.utils.Constants;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author tungck
 */
public class Entropy {
    
    public static float calculateEntropy(ArrayList<RowData> records){
        float entropy = 0.0f;
        if(records.size() == 0){
            return 0;
        }
        
        for (String label : Constants.LABEL) {
            int count = 0;
            for (int j = 0; j < records.size(); j++) {
                RowData r = records.get(j);
                if(r.getRow().get(15).equalsIgnoreCase(label)){
                    count++;  
                }
            }
            if(count > 0){
                float p = count/(float)records.size();
                entropy += -p * (Math.log(p) / Math.log(2));
            }
        }
        
        return entropy;
    }

}
